<?php

namespace addons\crontab\model;

use think\Model;
use think\Db;

class Crontab extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 定义字段类型
    protected $type = [
    ];
    // 追加属性
    protected $append = [
        'type_text'
    ];

    public static function getTypeList()
    {
        return [
            'url'   => __('Request Url'),
            'sql'   => __('Execute Sql Script'),
            'shell' => __('Execute Shell'),
        ];
    }

    public function getTypeTextAttr($value, $data)
    {
        $typelist = self::getTypeList();
        $value = $value ? $value : $data['type'];
        return $value && isset($typelist[$value]) ? $typelist[$value] : $value;
    }

    protected function setBegintimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setExecutetimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    //调用u8存储过程，获取自增序列号、流水号
    /*
     * array(1) {
      [0] => array(2) {
        ["Parent"] => string(10) "1000000001"
        ["Child"] => string(10) "1000000001"
      }
     *   */
    public static function getAutoIDByTname($table_name = 'RdRecord01'){
        $sql="declare @P1 int
        declare @P2 int
        exec sp_GetID @RemoteId = N'00', @cAcc_Id = N'001', @cVouchType = N'{$table_name}', @iAmount = 1, @iFatherId = @P1 output, @iChildId = @P2 output
        select @P1 as Parent, @P2 as Child";
        $arr = Db::connect('database.sec')->query($sql);
        
        if(!isset($arr[0]['Parent'])){
            exception('获取u8自增序列号失败');
        }
        
        return $arr[0]['Parent'];
    }
    
    /* 根据pda单号，获取uuid，得到单据前缀*/
    public static function getPdaFlagByCode($order_code){
        $pda_flag_arr = config('site.pda_flag');//设备类型数组
        $pda_flag = '';//设备标识
        if($order_code&&$pda_flag_arr){
            $uuid = db('pda_bill')->where('FBillNo',$order_code)->value('uuid');
            if($uuid){
                $pda_flag_arr = array_flip($pda_flag_arr);
                if(isset($pda_flag_arr[$uuid])){
                    $pda_flag = $pda_flag_arr[$uuid];
                }
            }
        }
        return $pda_flag;
    }
}
